/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Employee.java
 *
 * Created on Oct 30, 2013, 8:38:18 PM
 */
package sims.gui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.dao.EmployeeDao;
import sims.entity.TbEmployee;
import sims.library.Validate;

/**
 *
 * @author NINH
 */
public class Employee extends javax.swing.JPanel {

    EmployeeDao empDao;
    Validate valid;

    /** Creates new form Category */
    public Employee() {
        initComponents();
        empDao = new EmployeeDao();
        valid = new Validate();
        loadData();
        disableTextbox();
    }

    public void loadData() {
        tblEmployee.setModel(DisplayAll());
    }

    public DefaultTableModel DisplayAll() {
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        Vector vtable = new Vector();
        Vector vrow = new Vector();
        Vector vheader = new Vector();
        List<TbEmployee> emps = empDao.getAll(TbEmployee.class);

        vheader.add("Id");
        vheader.add("Name");
        vheader.add("Address");
        vheader.add("Username");
        vheader.add("Email");
        vheader.add("Phone");
        vheader.add("DOB");
        vheader.add("Gender");
        vheader.add("Role");

        for (TbEmployee emp : emps) {
            vrow.add(emp.getEmpId());
            vrow.add(emp.getFullname());
            vrow.add(emp.getAddress());
            vrow.add(emp.getUsername());
            vrow.add(emp.getEmail());
            vrow.add(emp.getPhone());
            vrow.add(format.format(new Date(emp.getDob().getTime())));
            vrow.add((emp.getGender() ? "Male" : "Female"));
            vrow.add(emp.getRoleName());
            vtable.add(vrow);
            vrow = new Vector();
        }
        return new DefaultTableModel(vtable, vheader);
    }

    public void Reset() {
        txtId.setText("");
        txtFullname.setText("");
        txtAddress.setText("");
        txtPhone.setText("");
        txtUsername.setText("");
        txtEmail.setText("");
        txtDob.setDate(null);
        pswPassword.setText("");
    }

    public void binding() {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        int row = tblEmployee.getSelectedRow();
        int id = Integer.parseInt(tblEmployee.getValueAt(row, 0).toString());

        TbEmployee emp = (TbEmployee) empDao.getById(TbEmployee.class, id);

        txtId.setText(id + "");
        txtFullname.setText(emp.getFullname());
        txtAddress.setText(emp.getAddress());
        txtDob.setDate(emp.getDob());
        txtEmail.setText(emp.getEmail());
        txtPhone.setText(emp.getPhone());
        txtUsername.setText(emp.getUsername());
        pswPassword.setText(emp.getPassword());
        if (emp.getGender() == true) {
            radMale.setSelected(true);
        } else {
            radFemale.setSelected(true);
        }
    }

    public void disableTextbox() {
        txtAddress.setEnabled(false);
        txtDob.setEnabled(false);
        txtEmail.setEnabled(false);
        txtFullname.setEnabled(false);
        txtPhone.setEnabled(false);
        txtUsername.setEnabled(false);
        pswPassword.setEnabled(false);
        radFemale.setEnabled(false);
        radMale.setEnabled(false);
        cboRole.setEnabled(false);
    }

    public void enableTextbox() {
        txtAddress.setEnabled(true);
        txtDob.setEnabled(true);
        txtEmail.setEnabled(true);
        txtFullname.setEnabled(true);
        txtPhone.setEnabled(true);
        txtUsername.setEnabled(true);
        pswPassword.setEnabled(true);
        radFemale.setEnabled(true);
        radMale.setEnabled(true);
        cboRole.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblId = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        lblFullname = new javax.swing.JLabel();
        txtFullname = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        lblUsername = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        lblDOB = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        lblGender = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        lblRole = new javax.swing.JLabel();
        cboRole = new javax.swing.JComboBox();
        pswPassword = new javax.swing.JPasswordField();
        radFemale = new javax.swing.JRadioButton();
        radMale = new javax.swing.JRadioButton();
        txtDob = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEmployee = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder("Employee manager"));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblId.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblId.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblId, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 10, 160, 20));

        txtId.setEnabled(false);
        jPanel1.add(txtId, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 10, 220, -1));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 180, -1, 24));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/edit.png"))); // NOI18N
        btnUpdate.setText("Edit");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 180, -1, 24));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 180, -1, 24));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Id: ");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 70, 20));

        lblFullname.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblFullname.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblFullname, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 40, 160, 20));
        jPanel1.add(txtFullname, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 40, 220, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel10.setText("Fullname: ");
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 70, 20));

        lblAddress.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblAddress.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 70, 160, 20));
        jPanel1.add(txtAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 70, 220, -1));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setText("Address: ");
        jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 70, 20));

        lblUsername.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblUsername.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 100, 160, 20));
        jPanel1.add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 100, 220, -1));

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel14.setText("Username: ");
        jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 70, 20));

        lblPassword.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblPassword.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 130, 160, 20));

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel16.setText("Password: ");
        jPanel1.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 70, 20));

        lblEmail.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblEmail.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 10, 160, 20));
        jPanel1.add(txtEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 10, 220, -1));

        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel18.setText("Email: ");
        jPanel1.add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 10, 70, 20));

        lblPhone.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblPhone.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 40, 160, 20));
        jPanel1.add(txtPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 40, 220, -1));

        jLabel20.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel20.setText("Phone: ");
        jPanel1.add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 40, 70, 20));

        lblDOB.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblDOB.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblDOB, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 70, 160, 20));

        jLabel22.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel22.setText("DOB: ");
        jPanel1.add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 70, 70, 20));

        lblGender.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblGender.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblGender, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 100, 160, 20));

        jLabel24.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel24.setText("Gender: ");
        jPanel1.add(jLabel24, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 100, 70, 20));

        jLabel25.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel25.setText("Role: ");
        jPanel1.add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 130, 70, 20));

        lblRole.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblRole.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblRole, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 130, 160, 20));

        cboRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrator", "Sale person", "Sale manager", "Inventory manager" }));
        jPanel1.add(cboRole, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 130, 220, -1));
        jPanel1.add(pswPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 130, 220, -1));

        buttonGroup1.add(radFemale);
        radFemale.setText("Female");
        jPanel1.add(radFemale, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 100, -1, -1));

        buttonGroup1.add(radMale);
        radMale.setText("Male");
        jPanel1.add(radMale, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 100, -1, -1));
        jPanel1.add(txtDob, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 70, 220, -1));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 1030, 260));

        tblEmployee.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblEmployee);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 1030, 250));
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (btnAdd.getText().equalsIgnoreCase("add")) {
            enableTextbox();
            Reset();
            btnAdd.setText("Save");
            tblEmployee.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnDelete.setText("Cancel");
        } else {
            try {
                String Fullname = txtFullname.getText();
                String Address = txtAddress.getText();
                String Username = txtUsername.getText();
                String Password = pswPassword.getPassword().toString();
                Date Dob = txtDob.getDate();
                String Email = txtEmail.getText();
                String Phone = txtPhone.getText();
                boolean gender = false;
                if (radMale.isSelected()) {
                    gender = true;
                }
                String roleName = cboRole.getSelectedItem().toString();

                TbEmployee emp = new TbEmployee();
                emp.setFullname(Fullname);
                emp.setAddress(Address);
                emp.setUsername(Username);
                emp.setPassword(Password);
                emp.setDob(Dob);
                emp.setEmail(Email);
                emp.setPhone(Phone);
                emp.setGender(gender);
                emp.setRoleName(roleName);

                empDao.insert(emp);
                JOptionPane.showMessageDialog(null, "Insert success.");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Insert error.");
            }
            loadData();
            disableTextbox();
            Reset();
            btnAdd.setText("Add");
            tblEmployee.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (btnDelete.getText().equalsIgnoreCase("delete")) {
            if (tblEmployee.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select employee to delete.");
                return;
            } else {
                int check = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (check == JOptionPane.OK_OPTION) {
                    int rowIndex = tblEmployee.getSelectedRow();
                    int empId = Integer.parseInt(tblEmployee.getValueAt(rowIndex, 0).toString());
                    TbEmployee emp = (TbEmployee) empDao.getById(TbEmployee.class, empId);
                    empDao.delete(emp);
                    JOptionPane.showMessageDialog(null, "Delete success.");
                    loadData();
                }
            }
        } else {
            disableTextbox();
            tblEmployee.setEnabled(true);
            btnAdd.setText("Add");
            btnAdd.setEnabled(true);
            btnUpdate.setText("Edit");
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
            Reset();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (btnUpdate.getText().equalsIgnoreCase("edit")) {
            if (tblEmployee.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select employee to edit.");
                return;
            } else {
                enableTextbox();
                binding();
                btnUpdate.setText("Save");
                tblEmployee.setEnabled(false);
                btnAdd.setEnabled(false);
                btnDelete.setText("Cancel");
            }
        } else {
            int id = Integer.parseInt(txtId.getText());
            String Fullname = txtFullname.getText();
            String Address = txtAddress.getText();
            String Username = txtUsername.getText();
            String Password = pswPassword.getPassword().toString();
            Date Dob = txtDob.getDate();
            String Email = txtEmail.getText();
            String Phone = txtPhone.getText();
            boolean gender = false;
            if (radMale.isSelected()) {
                gender = true;
            }
            String roleName = cboRole.getSelectedItem().toString();

            TbEmployee emp = new TbEmployee();
            emp.setEmpId(id);
            emp.setFullname(Fullname);
            emp.setAddress(Address);
            emp.setUsername(Username);
            emp.setPassword(Password);
            emp.setDob(Dob);
            emp.setEmail(Email);
            emp.setPhone(Phone);
            emp.setGender(gender);
            emp.setRoleName(roleName);

            empDao.update(emp);
            JOptionPane.showMessageDialog(null, "Update success.");
            loadData();
            disableTextbox();
            Reset();

            btnUpdate.setText("Edit");
            tblEmployee.setEnabled(true);
            btnAdd.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cboRole;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblDOB;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFullname;
    private javax.swing.JLabel lblGender;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblRole;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPasswordField pswPassword;
    private javax.swing.JRadioButton radFemale;
    private javax.swing.JRadioButton radMale;
    private javax.swing.JTable tblEmployee;
    private javax.swing.JTextField txtAddress;
    private com.toedter.calendar.JDateChooser txtDob;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullname;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
